package com.ag.oa.core.controller;

import com.ag.oa.core.entity.vo.SignVO;
import com.ag.oa.core.service.ISignService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping({"/sign", "/test/sign"})
public class SignController {

    @Autowired
    private ISignService signService;

    private static final Logger logger = LoggerFactory.getLogger(SignController.class);

    @RequestMapping(value = "/pageList", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getSignsData(Integer page, Integer pageSize) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            List<SignVO> voList = signService.getForSignPageListData(page, pageSize);
            res.put("signArrList", voList);
        }catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 保存打卡
     */
    @RequestMapping(value = "/saveWorkSign", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getSignInfo(Integer userId, String dateStr, Integer year, Integer month
            , Integer day) {

        JSONObject res = new JSONObject();
        int code;
        try {
            code = signService.saveUserNewWorkSign(userId, dateStr, year, month, day);
        }catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 获取打卡记录日期详情
     */
    @RequestMapping(value = "/dateDetails", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getSignDateDetailList(Integer userId, Integer page, Integer pageSize) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            List<SignVO> voList = signService.getUserSignedDateList(userId, page, pageSize);
            res.put("signDateList", voList);
        }catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 获取打卡记录日期详情
     */
    @RequestMapping(value = "/calDates", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getCalSignDateList(Integer userId, Integer year, Integer month) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            List<SignVO> voList = signService.getUserSignedCalDateList(userId, year, month);
            res.put("markersDate", voList);
        }catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 检查用户当前日期是否已签到, 并获取用户信息
     */
    @RequestMapping(value = "/user/hasSignedInfo", produces = "text/json;charset=UTF-8")
    public @ResponseBody String checkUserHasSignedInfo(Integer userId) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            signService.getUserHasSignedInfo(userId, res);
        }catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 根据日期获取当天签到总人数
     */
    @RequestMapping(value = "/signedTotal/date", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getSignedTotal(String dateStr) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            int total = signService.getUsersSignedTotalCountByDate(dateStr);
            res.put("signedTotal", total);
        }catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

}
